// pages/problem/problem.js

// 1. when in test.
// 2. when in correct book.
// 3. when in search.
const app = getApp();
const Util = require('../../utils/util.js');

Page({

    /**
     * 页面的初始数据
     */
    data: {
        description: '',
        options: [],
        optionState: [],
        analysis: '',
        needAnalysis: true,
        curIndex: 0,
        listLength: 0,
        exerciseMode: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.render();
    },

    generateStateArray: function (index, length) {
        let state = [];
        for (let i = 0; i < length; i++) {
            if (i == index) {
                state.push('/assets/checked.png');
            } else {
                state.push('/assets/unchecked.png');
            }
        }
        return state;
    },

    generateStateArray2: function (user, correct, length) {
        if (user == correct) {
            return this.generateStateArray(user, length);
        } 
        let state = [];
        for (let i = 0; i < length; i++) {
            if (i == user) {
                state.push('/assets/wrong.png');
            } else if (i == correct) {
                state.push('/assets/checked.png');
            } else {
                state.push('/assets/unchecked.png');
            }
        }
        return state;
    },

    getAnswerIndex: function (options) {
        let index = 0;
        for (; index < options.length; index++) {
            if (options[index].isRight) {
                break;
            }
        }
        return index;
    },

    render: function () {
        console.log(app.globalData.problemList);
        let curIndex = app.globalData.problemIndex;
        let problem = app.globalData.problemList[curIndex];
        let listLength = app.globalData.problemList.length;
        if (app.globalData.quizFinished) {
            this.setData({
                curIndex: curIndex,
                listLength: listLength,
                options: problem.optionList,
                description: problem.name,
                analysis: problem.analysis,
                needAnalysis: true,
                optionState: this.generateStateArray2(problem.answer, this.getAnswerIndex(problem.optionList), problem.optionList.length),
                exerciseMode: app.globalData.exerciseMode
            });
        } else {
            this.setData({
                curIndex: curIndex,
                listLength: listLength,
                options: problem.optionList,
                description: problem.name,
                needAnalysis: false,
                optionState: this.generateStateArray(problem.answer, problem.optionList.length),
                exerciseMode: app.globalData.exerciseMode
            });
        }
    },

    tapOption: function (e) {
        this.radioSelected = parseInt(e.currentTarget.dataset.index);
        let index = this.data.curIndex;
        let length = this.data.options.length;
        app.globalData.problemList[index].answer = this.radioSelected;
        this.setData({
            optionState: this.generateStateArray(this.radioSelected, length)
        })
    },

    showResult: function () {
        let problems = app.globalData.problemList;
        app.globalData.quizFinished = true;
        let score = 0;
        for (let i = 0; i < problems.length; i++) {
            if (problems[i].answer && problems[i].optionList[problems[i].answer].isRight) {
                score += 1;
            }
        }
        wx.showModal({
            title: "作答结果",
            content: `您答对了${score}道题，总共${problems.length}道题。`,
            complete: () => {
                this.render();
            }
        })
    },

    submit: function (e) {
        wx.showModal({
            title: "提交答案",
            content: "是否确定已经完成作答？一旦点击确定，将立即给出成绩且无法修改解答。",
            success: (e) => {
                if (e.confirm) {
                    this.showResult();
                }
            }
        })
    },

    prevProblem: function (e) {
        app.backward();
        this.render();
    },

    nextProblem: async function (e) {
        if (app.globalData.exerciseMode && this.data.curIndex == this.data.listLength - 1) {
            wx.showLoading();
            let b = app.globalData.baseURL;
            let resp = await Util.asyncHttpPost(b + 'randomQuestion', {
                groupName: app.globalData.groupName,
                gradeName: app.globalData.gradeName,
                subject: app.globalData.subject,
                tenantId: app.globalData.tenantId
            });
            let question = resp.data.data;
            app.globalData.problemList.push(question);
            app.forward(-1);
            wx.hideLoading();
            this.render();
        } else {
            app.forward(-1);
            this.render();
        }
    },

    addToWrongBook: async function (e) {
        wx.showLoading();
        await app.addToWrongBook();
        wx.hideLoading();
        wx.showModal({
            title: "添加成功"
        });
    }
})